package com.example.demo.Dao;

import com.example.demo.bean.User;
import com.example.demo.service.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


import java.util.List;


@Repository
public class UserDaoImpl implements UserDao {


    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
  /*  public void add(User user) {
        jdbcTemplate.update("insert into guest(score,level,balance) values(?, ?, ?)",
                user.getName(),user.getAmount(),user.getPrice());
    }*/
    public void add(User user) {
        jdbcTemplate.update("insert into guest(score,level,balance) values(?, ?, ?)",
                user.getScore(),user.getLevel(),user.getBalance());
    }

    @Override
    public void del(Long id) {
        jdbcTemplate.update("delete from guest where id=?",
                id);
    }


    @Override
   /* public int update(User user) {
        return (int)jdbcTemplate.update("update guest set score=?,level=?,level=? where id=?",
                user.getName(),user.getAmount(),user.getPrice(),user.getId());
    }*/
    public int update(User user) {
        return (int)jdbcTemplate.update("update guest set score=?,level=?,balance=? where id=?",
                user.getScore(),user.getLevel(),user.getBalance(),user.getId());
    }

    @Override
    public User findUserById(Long id) {
        List<User> userList = jdbcTemplate.query("select * from guest where id=?",new Object[]{id},new BeanPropertyRowMapper(User.class));
        if(userList!=null && userList.size()>0){
            return  userList.get(0);
        }else{
            return null;
        }
    }


    @Override
    public List<User> findUserList() {
        List<User> userList = jdbcTemplate.query("select * from guest ",new Object[]{},new BeanPropertyRowMapper(User.class));
        if(userList!=null && userList.size()>0){
            return  userList;
        }else{
            return null;
        }
    }
}
